//
//  ZYXNSThread1ViewController.m
//  MultiThreading
//  NSThread演示
//

#import "ZYXNSThread1ViewController.h"

@interface ZYXNSThread1ViewController ()

- (IBAction)btnClick;

@end


@implementation ZYXNSThread1ViewController

- (IBAction)btnClick {
    // 1.获得当前的线程
    NSThread *current = [NSThread currentThread];
    NSLog(@"btnClick---%@", current);
    
    // 2.获得主线程
    NSThread *main = [NSThread mainThread];
    NSLog(@"btnClick---%@", main);
    
    // 3.执行一些耗时操作 : 创建一条子线程
    //[self threadCreate];
    //[self threadCreate2];
    [self threadCreate3];
}

/**
 * NSThread的创建方式
 * 1> 先创建初始化线程
 * 2> start开启线程
 */
- (void)threadCreate
{
    NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"哈哈"];
    thread1.name = @"线程A";
    // 开启线程
    [thread1 start];
    
    NSThread *thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"嘻嘻"];
    thread2.name = @"线程B";
    // 开启线程
    [thread2 start];
}

/**
 * 子线程执行的操作
 */
- (void)run:(NSString *)param
{
    NSThread *current = [NSThread currentThread];
    
    for (int i = 0; i<5; i++) {
        NSLog(@"run---%@---%@", current, param);
    }
}

// 2条子线程执 run: 方法 出现了资源抢夺现象

//2015-03-31 17:09:22.812 MultiThreading[18526:258612] btnClick---<NSThread: 0x7b71eb90>{number = 1, name = main}
//2015-03-31 17:09:22.814 MultiThreading[18526:258612] btnClick---<NSThread: 0x7b71eb90>{number = 1, name = main}
//2015-03-31 17:09:22.814 MultiThreading[18526:258699] run---<NSThread: 0x7b7432c0>{number = 2, name = 线程A}---哈哈
//2015-03-31 17:09:22.815 MultiThreading[18526:258699] run---<NSThread: 0x7b7432c0>{number = 2, name = 线程A}---哈哈
//2015-03-31 17:09:22.815 MultiThreading[18526:258699] run---<NSThread: 0x7b7432c0>{number = 2, name = 线程A}---哈哈
//2015-03-31 17:09:22.815 MultiThreading[18526:258700] run---<NSThread: 0x7b71ed40>{number = 3, name = 线程B}---嘻嘻
//2015-03-31 17:09:22.815 MultiThreading[18526:258699] run---<NSThread: 0x7b7432c0>{number = 2, name = 线程A}---哈哈
//2015-03-31 17:09:22.816 MultiThreading[18526:258700] run---<NSThread: 0x7b71ed40>{number = 3, name = 线程B}---嘻嘻
//2015-03-31 17:09:22.816 MultiThreading[18526:258699] run---<NSThread: 0x7b7432c0>{number = 2, name = 线程A}---哈哈
//2015-03-31 17:09:22.816 MultiThreading[18526:258700] run---<NSThread: 0x7b71ed40>{number = 3, name = 线程B}---嘻嘻
//2015-03-31 17:09:22.816 MultiThreading[18526:258700] run---<NSThread: 0x7b71ed40>{number = 3, name = 线程B}---嘻嘻
//2015-03-31 17:09:22.839 MultiThreading[18526:258700] run---<NSThread: 0x7b71ed40>{number = 3, name = 线程B}---嘻嘻




/**
 * NSThread的创建方式
 * 创建完线程直接(自动)启动              | 不能设置当前线程名称
 */
- (void)threadCreate2
{
    // withObject:@"我是参数" 传递给 run: 方法
    [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"我是参数"];
}


/**
 * NSThread的创建方式
 * 隐式创建线程, 并且直接(自动)启动       | 不能设置当前线程名称
 */
- (void)threadCreate3
{
    // 在后台线程中执行 === 在子线程中执行
    [self performSelectorInBackground:@selector(run:) withObject:@"abc参数"];
}






@end
